package com.evola.rm.view.newprojectwizard
{
	import com.evola.rm.model.Language;
	import com.evola.rm.model.Project;
	import com.evola.rm.utils.XMLConvertor;
	import com.evola.rm.view.newprojectwizard.util.NewProjectWizardLanguage;
	
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.FileFilter;
	
	import mx.collections.ArrayCollection;
	import mx.managers.PopUpManager;
	import mx.utils.StringUtil;

	[Bindable]
	public class NewProjectWizardPresenter
	{

		public var model:NewProjectWizardModel=new NewProjectWizardModel();
		public var view:NewProjectWizardView;

		public function NewProjectWizardPresenter()
		{
		}

		public function init():void
		{

			model.init();
		}

		public function chooseProjectLocation():void
		{

			var file:File=new File();
			file.addEventListener(Event.SELECT, onFolderSelected);
			file.browseForDirectory("Select new project location...");
		}

		protected function onFolderSelected(event:Event):void
		{
			var file:File=event.target as File;
			file.removeEventListener(Event.SELECT, onFolderSelected);

			model.projectLocation=file.nativePath;
		}

		public function showLanguageView():void
		{

			var projectName:String=StringUtil.trim(model.projectName);
			var projectLocation:String=StringUtil.trim(model.projectLocation);

			if (!projectLocation || !projectName)
			{

				model.projectInfoError="You must enter project name and location.";
			}
			else
			{

				model.projectInfoError = null;
				model.viewIndex=1;
			}
		}

		public function showProjectInfoView():void
		{

			model.viewIndex=0;
		}

		public function finish():void
		{

			var projectName : String = StringUtil.trim(model.projectName);
			var projectLocation : String = StringUtil.trim(model.projectLocation);
			
			var project : Project = new Project();
			
			project.name = projectName;
			
			var languages : ArrayCollection = new ArrayCollection();

			var errorString : String = "";
			var count : int = 1;
			
			for each(var lang : NewProjectWizardLanguage in model.languageFiles){
				
				var langName : String = StringUtil.trim(lang.name);
				var langPath : String = StringUtil.trim(lang.path);
				var langCode : String = StringUtil.trim(lang.code);
				
				if(!langName || !langPath || !langCode){
					
					if(errorString)
						errorString += ", ";
					
					errorString += count+""; 
				}
				else{
					
					var language : Language = new Language();
					language.code = langCode;
					language.name = langName;
					language.path = langPath;
					
					languages.addItem(language);
				}
				
				count++;
			}
			
			if(errorString){
				
				model.languagesError = "You have errors on languages "+errorString;
				return;
			}
			
			project.languages = languages;
			
			var projectXml : XML = XMLConvertor.exportToXml(project);
			
			//trace(projectXml.toXMLString());
			
			var file : File = new File(projectLocation);
			
			var projectFile:File = file.resolvePath(projectName+".rmp");
			
			var fileStream : FileStream = new FileStream();
			fileStream.openAsync(projectFile, FileMode.WRITE);
			
			fileStream.writeUTFBytes(projectXml);
			fileStream.close();
			
			close();
			
			MainPresenter.instance.openNewProject(projectFile);
		}

		public function addLanguage():void
		{

			var language:NewProjectWizardLanguage=new NewProjectWizardLanguage();

			model.languageFiles.addItem(language);
		}

		public function removeLanguage(language:NewProjectWizardLanguage):void
		{

			var index:int=model.languageFiles.getItemIndex(language);
			model.languageFiles.removeItemAt(index);
		}

		public function close():void
		{

			PopUpManager.removePopUp(view);
		}
		
		public function chooseLanguageFile(language : NewProjectWizardLanguage) : void{
			
			var file:File=new File();
			var fileFilter:FileFilter=new FileFilter("Language Properties File (*.properties)", "*.properties");
			
			file.addEventListener(Event.SELECT, function(event : Event) : void{
			
				var file : File = event.target as File;
				
				language.path = file.nativePath;
			});
			
			file.browseForOpen("Select language file 'properties'", [fileFilter]);
		}

	}
}
